package com.usian.admin.service.impl;


import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper,AdSensitive> implements AdSensitiveService {
    @Override
    public ResponseResult findByNameAndPage(SensitiveDto dto) {
        //参数校验
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //设置分页条件
        //初始化分页参数
        dto.checkParam();
        Page<AdSensitive> page = new Page<>(dto.getPage(),dto.getSize());

        //设置查询条件
        String name = dto.getName();
        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper();
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like(AdSensitive::getSensitives,name);
        }

        //执行查询
        IPage<AdSensitive> resultPage = page(page, queryWrapper);
        //封装返回的分页结果数据
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),resultPage.getTotal());
        //返回当前页结果集
        responseResult.setData(resultPage.getRecords());
        return responseResult;
    }
}
